package com.web.base.auth.service;

import com.blue.core.tree.TreeBuilder;
import com.web.base.auth.dao.*;
import com.web.base.auth.model.Menu;
import com.web.base.auth.model.RoleMenu;
import com.web.base.auth.model.RoleMenuAuth;
import com.web.base.auth.model.RoleMenuButton;
import com.web.core.util.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 菜单业务层
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2017年04月15日
 */
@Service
@Transactional
public class MenuService
{
	@Autowired
	private MenuDao menuDao;
	@Autowired
	private MenuAuthDao menuAuthDao;
	@Autowired
	private MenuButtonDao menuButtonDao;
	@Autowired
	private RoleMenuDao roleMenuDao;
	@Autowired
	private RoleMenuAuthDao roleMenuAuthDao;
	@Autowired
	private RoleMenuButtonDao roleMenuButtonDao;
	
	public MenuService()
	{
	}
	
	public Menu save(Menu menu)
	{
		if (menu.getId() == null)
		{
			menuDao.save(menu);
		}
		else
		{
			menuDao.update(menu);
		}
		Menu a = menuDao.get(menu.getId());
		return a;
	}
	
	public Menu get(Integer id)
	{
		Menu log = menuDao.get(id);
		return log;
	}
	
	public Map<String, List<Menu>> list(Menu menu)
	{
		List<Menu> list = menuDao.list(menu);
		return TreeBuilder.buildMenu(list);
	}
	
	public Map<String, List<Menu>> index(Integer userId)
	{
		List<Menu> list = menuDao.listByUser(userId);
		return TreeBuilder.buildMenu(list);
	}
	
	public void addRoleMenu(Integer roleId, String menuIds, String authIds, String buttonIds)
	{
		roleMenuDao.deleteBy("roleId", roleId);
		if (menuIds != null && !menuIds.isEmpty())
		{
			List<RoleMenu> rmList = Stream.of(menuIds.split(Const.ID_LIST_SPLIT))
					.map(e -> new RoleMenu(roleId, Integer.valueOf(e)))
					.collect(Collectors.toList());
			roleMenuDao.saveList(rmList);
		}

		roleMenuAuthDao.deleteBy("roleId", roleId);
		if (authIds != null && !authIds.isEmpty())
		{
			List<RoleMenuAuth> rmaList = Stream.of(authIds.split(Const.ID_LIST_SPLIT))
					.map(e -> new RoleMenuAuth(roleId, Integer.valueOf(e)))
					.collect(Collectors.toList());
			roleMenuAuthDao.saveList(rmaList);
		}

		roleMenuButtonDao.deleteBy("roleId", roleId);
		if (buttonIds != null && !buttonIds.isEmpty())
		{
			List<RoleMenuButton> rmbList = Arrays.stream(buttonIds.split(Const.ID_LIST_SPLIT))
					.map(e -> new RoleMenuButton(roleId, Integer.valueOf(e)))
					.collect(Collectors.toList());
			roleMenuButtonDao.saveList(rmbList);
		}
	}
	
	public Map<String, List<Menu>> listSelect(Integer roleId, Integer appId)
	{
		List<Menu> list = roleMenuDao.listSelect(roleId, appId);
		return TreeBuilder.buildMenu(list);
	}

	public void delete(Integer id)
	{
		roleMenuButtonDao.deleteByMenu(id);
		roleMenuAuthDao.deleteByMenu(id);
		roleMenuDao.deleteBy("menuId", id);
		menuButtonDao.deleteBy("menuId", id);
		menuAuthDao.deleteBy("menuId", id);
		menuDao.deleteId(id);
	}
	
}
